package com.lcxw.hxzbapi.dao.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.lcxw.hxzbapi.entity.pojo.YwpdChargeDetail;

@Mapper
public interface YwpdChargeDetailMapper {

    /**
     * 1、新增一条充值记录
     */
    @Insert({
        "insert into ywpd_chargedetail (userId, orderNo, ",
        "chargeMode, thirdOrderNo, chargeTime, ",
        "thirdBuyerAccount, thirdSellerAccount, thirdBuyerEmail, thirdSellerEmail, ",
        "rmb, coin, content, status, ",
        "proxyUId,dealId)",
        "values (#{userId}, #{orderNo}, ",
        "#{chargeMode}, #{thirdOrderNo},  #{chargeTime}, ",
        "#{thirdBuyerAccount}, #{thirdSellerAccount},  #{thirdBuyerEmail}, #{thirdSellerEmail}, ",
        "#{rmb}, #{coin},  #{content}, #{status}, ",
        "#{proxyUId}, #{dealId})"
    })
    int addChargeDetail(YwpdChargeDetail ywpdChargedetail);

    /**
     * 2、用orderNo删除一条记录
     */
    @Delete({"DELETE FROM ywpd_chargedetail WHERE orderNo=#{orderNo}"})
    int deleteByOrderNo(String orderNo);

    /**
     * 3、通过orderNo更新一条充值记录的交易状态
     */
    @Update({
        "update ywpd_chargedetail",
        "set status = #{status},",
        "thirdOrderNo = #{thirdOrderNo},",
        "chargeTime = #{chargeTime},",
        "thirdBuyerAccount = #{thirdBuyerAccount},",
        "thirdSellerAccount = #{thirdSellerAccount},",
        "thirdBuyerEmail = #{thirdBuyerEmail},",
        "thirdSellerEmail = #{thirdSellerEmail},",
        "coin = #{coin},",
        "content = #{content},",
        "proxyUId = #{proxyUId},",
        "dealId = #{dealId}",
        "where orderNo = #{orderNo}"
    })
    int updateStatusByOrderNo(YwpdChargeDetail ywpdChargedetail);

    /**
     * 4、用orderNo查询一条记录
     */
    @Select("SELECT * FROM ywpd_chargedetail WHERE orderNo=#{orderNo}")
    YwpdChargeDetail selectByOrderNo(String orderNo);

    /**
     * 5、获取用户充值的人民币总额
     */
    @Select("SELECT SUM(rmb) FROM ywpd_chargedetail WHERE userId=#{userId} and status='交易成功'")
    float selectRmbCount(long userId);

    /**
     * 6、获取用户充值的虚拟货币总额
     */
    @Select("SELECT SUM(coin) FROM ywpd_chargedetail WHERE userId=#{userId} and status='交易成功'")
    int selectCoinCount(long userId);

    /**
     * 7、动态查询充值记录
     */
    @Select("<script>" + "SELECT * FROM ywpd_chargedetail WHERE "
        + "<if test='userId!=0'>"
        + "userId = #{userId} and "
        + "</if>"
        + "<if test='chargeMode!=null'>"
        + "chargeMode=#{chargeMode} and "
        + "</if>"
        + "<if test='startTime!=null'>"
        + "chargeTime &gt;= #{startTime} and "
        + "</if>"
        + "<if test='endTime!=null'>"
        + "chargeTime &lt;= #{endTime} and "
        + "</if>"
        + "<if test='status!=null'>"
        + "status=#{status} and "
        + "</if>"
        + "1=1"
        + " LIMIT #{pageIndex},#{pageSize} "
        + "</script>")
    List<YwpdChargeDetail> selectDync(@Param("userId") long userId,
        @Param("chargeMode") String chargeMode, @Param("startTime") String startTime,
        @Param("endTime") String endTime, @Param("status") String status,
        @Param("pageIndex") int pageIndex, @Param("pageSize") int pageSize);

    /**
     * 7、动态查询充值记录的个数
     */
    @Select("<script>" + "SELECT COUNT(*) FROM ywpd_chargedetail WHERE "
        + "<if test='userId!=0'>"
        + "userId = #{userId} and "
        + "</if>"
        + "<if test='chargeMode!=null'>"
        + "chargeMode=#{chargeMode} and "
        + "</if>"
        + "<if test='startTime!=null'>"
        + "chargeTime &gt;= #{startTime} and "
        + "</if>"
        + "<if test='endTime!=null'>"
        + "chargeTime &lt;= #{endTime} and "
        + "</if>"
        + "<if test='status!=null'>"
        + "status=#{status} and "
        + "</if>"
        + "1=1"
        + "</script>")
    int selectCountDync(@Param("userId") long userId, @Param("chargeMode") String chargeMode,
        @Param("startTime") String startTime, @Param("endTime") String endTime,
        @Param("status") String status);

    /**
     * 8、通过用户Id查询充值记录
     */
    @Select("SELECT * FROM ywpd_chargedetail WHERE userId = #{userId} AND status = '交易成功' ORDER BY id DESC LIMIT #{pageNum},#{pageSize} ")
    List<YwpdChargeDetail> selectByUserId(long userId, int pageNum, int pageSize);
}
